package com.example.finalassessment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.finalassessment.dao.CategoryMapper;
import com.example.finalassessment.model.Category;
import com.example.finalassessment.model.Product;
import com.example.finalassessment.dao.ProductMapper;
import com.example.finalassessment.model.User;
import com.example.finalassessment.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.finalassessment.util.ExceptionCodeMsgEnum;
import com.example.finalassessment.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author funny_chao
 * @since 2024-12-09
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Product getById(Integer id) {
        // 通过 MyBatis-Plus 提供的 baseMapper 获取数据
        Product product = this.baseMapper.selectById(id); // 使用 baseMapper 来查询数据库
        if (product == null) {
            return null; // 商品未找到
        }
        return product;
    }


    @Override
    public ResponseData editProduct(Integer id, Product product) {
//        Category category = categoryMapper.selectById(product.getCategoryId());
        Product updatedProduct = productMapper.selectById(id);
        System.out.println("选择的数据"+updatedProduct);
//        if (category == null) {
//            return ResponseData.error(ExceptionCodeMsgEnum.CATEGORY_NOT_FOUND);
//        }
        if (updatedProduct == null) {
            return ResponseData.error(ExceptionCodeMsgEnum.PRODUCT_NOT_FOUND);
        }

        System.out.println("!!!!!!!!!!!!!!!!!!!!"+product.getCategoryId());
        // 更新商品字段
        updatedProduct.setName(product.getName());
        updatedProduct.setPrice(product.getPrice());
        updatedProduct.setStock(product.getStock());
        updatedProduct.setCategoryId(product.getCategoryId());

        // 执行更新操作
        int rows = productMapper.updateById(updatedProduct);
        if (rows > 0) {
            return ResponseData.success(updatedProduct); // 返回更新后的商品数据
        } else {
            return ResponseData.error(ExceptionCodeMsgEnum.UPDATE_FAILED);
        }
    }

    @Override
    public ResponseData deleteProductById(Integer id) {
        boolean isDeleted = this.removeById(id);

        if (!isDeleted) {
            // 删除失败，返回错误响应
            return ResponseData.error(ExceptionCodeMsgEnum.DELETE_FAILED);
        }

        // 删除成功，返回成功响应
        return ResponseData.success("删除成功", null);
    }

    @Override
    public ResponseData addProduct(Product product) {
        // 检查商品名称是否已存在
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        Product existingProduct = productMapper.selectOne(queryWrapper.eq("name", product.getName()));
        if (existingProduct != null) {
            return ResponseData.error(ExceptionCodeMsgEnum.PRODUCT_ALREADY_EXISTS);
        }
        // 添加商品
        int rows = productMapper.insert(product);
        if (rows > 0) {
            return ResponseData.success("商品添加成功", product);
        } else {
            return ResponseData.error(ExceptionCodeMsgEnum.ADD_FAILED);
        }
    }
}
